﻿using BaoVeThanhDia.GamePlay.Monsters;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace BaoVeThanhDia.GamePlay.Towers
{
    class BasicTower : Tower
    {
        private float countDraw;// biến phụ đếm thời gianư
        Random rand;
        int countX2;
        public BasicTower() { }
        public BasicTower(Vector2 index, int level, int type)
        {
            sound = new Sound.CustomSoundEffect("sounds/effect/Tower/Mui_Ten");
            ValueBuff = 0;
            rand = new Random();
            countDraw = 0;
            setIndex(index);
            setLevel(level, type);
            setelement();
            weapons = new List<Weapon>();
            countX2 = 0;
        }
        public override void Load(ContentManager content)
        {
            sound.Load(content);
            scale = 128 / 100f;
            Image = content.Load<Texture2D>("images/towers/basic/" + Level.ToString() + (Type == 0 ? "" : Type.ToString()));
        }
        public override void setLevel(int level, int type)
        {
            Level = level;
            Type = type;
            if (Type == 1) Effect = "Poison";
            else if (Type == 2) Effect = "Slow";
            else Effect = "None";
        }
        public override void Draw(ContentManager cont, SpriteBatch sprite)
        {
            if (ValueBuff > 0)
            {
                ImageBuffed = cont.Load<Texture2D>("images/towers/soul/root" + (ValueBuff < 30 ? "" : (ValueBuff < 50 ? "1" : "2")));
                sprite.Draw(ImageBuffed, (Position + new Vector2(-13, -45)) * GamePage.Scale + GamePage.Dolech, new Rectangle(((int)countDraw / 5) % 6 * 100, 0, 100, 100), Color.White, 0f, Vector2.Zero, 1.5f * GamePage.Scale, SpriteEffects.None, 0f);
            }
            DrawWeapon(cont, sprite);
            sprite.Draw(Image, (Position + new Vector2(0, -80)) * GamePage.Scale + GamePage.Dolech, new Rectangle((int)(countDraw / 5) * 100, 0, 100, 141), Color.White, 0f, Vector2.Zero, scale * GamePage.Scale, SpriteEffects.None, 0f);
            countDraw += Speed * GamePage.Speed / 2;
            if (countDraw >= 30) { countDraw = 0; }
        }
        public override void OptionDraw(SpriteBatch sprite, Vector2 posi, Vector2 dolech, float scale, bool isTouch)
        {
            sprite.Draw(Image, posi + dolech, new Rectangle((int)(countDraw / 5) * 100, 0, 100, 141), Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
            if (isTouch)
            {
                countDraw += Speed;
                if (countDraw >= 30) { countDraw = 0; }
            }
        }
        public override void setelement()
        {
            if (Level == 1)
            {
                Price = 40;
                PhysicDame = 20;
                MagicDame = 2;
                Speed = 1;
                Range = 1.5f;
                countEffect = 0;
                PriceBought = 40;
            }
            else if (Level == 2)
            {
                Price = 20;
                Speed = 1;
                Range = 1.75f;
                MagicDame = 3;
                PriceBought = 60;
                if (Type == 1)
                {
                    countEffect = 1;
                    PhysicDame = 10;
                }
                else
                {
                    countEffect = 30;
                    PhysicDame = 22;
                }
            }
            else
            {
                Price = 30;
                Speed = 1;
                Range = 2f;
                PriceBought = 90;
                if (Type == 1)
                {
                    countEffect = 2;
                    PhysicDame = 5;
                    MagicDame = 2;
                }
                else
                {
                    countEffect = 60;
                    PhysicDame = 24;
                    MagicDame = 5;
                }
            }
            getUpGradeValue();
        } // dành riêng cho tháp cơ bản
        public override void Ban()
        {
            if ((int)countDraw / 5 == 4 && (int)countDraw % 5 == 0)
            {
                bool isX2 = false;
                if (countX2 == 15 && rand.Next(10000) % 100 < 15)
                {
                    isX2 = true;
                }
                for (int i = 0; i < GamePage.monsters.Count; i++)
                {
                    if (Vector2.Distance(Position, GamePage.monsters[i].getPosition()) <= Range * 128 && GamePage.monsters[i].getCurHP() > 0)
                    {
                        sound.Play();
                        weapons.Add(new Weapon(1, PhysicDame * (isX2 ? 2 : 1) * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), MagicDame * (isX2 ? 2 : 1) * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), 25, Position + new Vector2(64, 0) * GamePage.Scale, GamePage.monsters[i].getPosition() + new Vector2(64, 64) * GamePage.Scale, i, Effect, Type == 1 ? 15 : 60, countEffect));
                        break;
                    }
                }
            }
        }
        public override float getPriceUpGrade(int level, int type)
        {
            return new BasicTower(Vector2.Zero, level, type).Price;
        }
        public override void DrawPictureUpGrade(int level, int type, ContentManager cont, SpriteBatch sprite, Vector2 posi)
        {
            BasicTower temp = new BasicTower(Vector2.Zero, level, type);
            temp.Load(cont);
            temp.OptionDraw(sprite, posi + new Vector2(6, -10) * GamePage.Scale, GamePage.Dolech, 0.8f * GamePage.Scale, false);
        }
        private void getUpGradeValue()
        {
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[8])
            {
                Price = (Price * 3) / 4;
                PriceBought = (PriceBought * 3) / 4;
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[9])
            {
                Speed += Speed / 5;
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[10])
            {
                countX2 = 15;
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[11])
            {
                Range *= 1.5f;
            }
        }
    }
}
